<template>
  <div>
    <h2>教师管理</h2>
    <el-table :data="teachers" style="width: 100%">
      <el-table-column prop="id" label="工号" width="120" />
      <el-table-column prop="name" label="姓名" />
      <el-table-column prop="title" label="职称" />
      <el-table-column prop="department" label="院系" />
      <el-table-column label="操作" width="120">
        <template #default="scope">
          <el-button size="mini" @click="editTeacher(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteTeacher(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 编辑教师弹窗 -->
    <el-dialog title="编辑教师" v-model="dialogVisible" width="400px">
      <el-form :model="editForm" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="editForm.name" />
        </el-form-item>
        <el-form-item label="职称">
          <el-input v-model="editForm.title" />
        </el-form-item>
        <el-form-item label="院系">
          <el-input v-model="editForm.department" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveTeacher">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'

const teachers = ref([
  { id: 'T001', name: '王老师', title: '副教授', department: '计算机学院' },
  { id: 'T002', name: '赵老师', title: '讲师', department: '软件学院' }
])

const dialogVisible = ref(false)
const editForm = ref({ id: '', name: '', title: '', department: '' })

function editTeacher(row) {
  editForm.value = { ...row }
  dialogVisible.value = true
}
function saveTeacher() {
  // TODO: 调用后端接口保存
  ElMessage.success('保存成功')
  dialogVisible.value = false
}
function deleteTeacher(row) {
  // TODO: 调用后端接口删除
  teachers.value = teachers.value.filter(t => t.id !== row.id)
  ElMessage.success('删除成功')
}
</script>